const router = require('koa-router')()
const {getArea, postArea, patchArea, deleteArea} = require('../../controller/area')
const {loginCheck} = require('../../middlewares/loginChecks')

router.prefix('/api/area')

router.get('/area', loginCheck, async (ctx, next) => {
  ctx.body = await getArea()
})

router.post('/area', loginCheck, async (ctx, next) => {
  const {name, parentId, level} = ctx.request.body
  ctx.body = await postArea({name, parentId, level})
})

router.patch('/area', loginCheck, async (ctx, next) => {
  const {name, id} = ctx.request.body
  ctx.body = await patchArea({name, id})
})

router.delete('/area/:id', loginCheck, async (ctx, next) => {
  const {id} = ctx.params
  ctx.body = await deleteArea(id)
})

module.exports = router
